package com.bbs.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.bbs.entity.Moderator;
import com.bbs.entity.User;
import com.bbs.util.DBHelp;
import com.bbs.util.Page;
import com.bbs.util.RowMapper;

public class Userdao {
	private DBHelp<User> dh = new DBHelp<User>();

	public User findByNameAndPwd(String name,String pwd) {
		String sql = "select * from t_user where username = ? and userpwd = ?";
		return dh.queryForObject(sql, new UserMapper(), name,pwd);
	}
	
	public int save(User u){
		String sql = "INSERT INTO t_user(username,userpwd,createdate,email) VALUES(?,?,?,?)";
		return dh.update(sql, u.getUsername(),u.getUserpwd(),u.getCreatedate(),u.getEmail());
	}

	public User findByName(String name) {
		String sql = "select id,username,userpwd,score,level,headpic,createdate,email,role,state,active from t_user where username = ?";
		return dh.queryForObject(sql, new UserMapper(), name);
	}

	/**
	 * 通过用户名和密码查找管理员账号
	 * @param name
	 * @param pwd
	 * @return
	 */
	public User findManager(String name, String pwd) {
		String sql = "select * from t_user where username = ? and userpwd = ? and role = 3";
		return dh.queryForObject(sql, new UserMapper(), name,pwd);
	}


	public List<User> findAllUser(Page p) {
		String sql = "SELECT id,username,userpwd,score ,LEVEL,headpic,createdate,email,role,state,state,active FROM t_user WHERE role = 1 LIMIT ?,?";
		return dh.queryForList(sql, new UserMapper(),p.getStartNum(),p.getPageSize());
	}

	public List<User> findModerators(Page p) {
		String sql = "SELECT id,username,userpwd,score,LEVEL,headpic,createdate,email,role,state,active FROM t_user WHERE role = 2 limit ?,?";
		return dh.queryForList(sql, new RowMapper<User>(){

			public User maprow(ResultSet rs) throws SQLException {
				User u = new User();
				u.setActive(rs.getInt("active"));
				u.setCreatedate(rs.getString("createdate"));
				u.setEmail(rs.getString("email"));
				u.setHeadpic(rs.getString("headpic"));
				u.setId(rs.getInt("id"));
				u.setLevel(rs.getInt("level"));
				u.setRole(rs.getInt("role"));
				u.setScore(rs.getInt("score"));
				u.setUsername(rs.getString("username"));
				u.setUserpwd(rs.getString("userpwd"));
				
				List<Moderator> list = new ModeratorDao().findModeratorsByUid(rs.getInt("id")); 
				u.setMlist(list);
				return u;
			}
			
		},p.getStartNum(),p.getPageSize());
	}
	
	/**
	 * 根据版主id查找版主及其管理的板块
	 * @param id
	 * @return
	 */
	public User findModerator(int id) {
		String sql = "SELECT id,username,userpwd,score,LEVEL,headpic,createdate,email,role,state,active FROM t_user WHERE role = 2 and id = ?";
		return dh.queryForObject(sql, new RowMapper<User>(){

			public User maprow(ResultSet rs) throws SQLException {
				User u = new User();
				u.setActive(rs.getInt("active"));
				u.setCreatedate(rs.getString("createdate"));
				u.setEmail(rs.getString("email"));
				u.setHeadpic(rs.getString("headpic"));
				u.setId(rs.getInt("id"));
				u.setLevel(rs.getInt("level"));
				u.setRole(rs.getInt("role"));
				u.setScore(rs.getInt("score"));
				u.setUsername(rs.getString("username"));
				u.setUserpwd(rs.getString("userpwd"));
				
				List<Moderator> list = new ModeratorDao().findModeratorsByUid(rs.getInt("id")); 
				u.setMlist(list);
				return u;
			}
			
		},id);
	}

	public User findById(int id) {
		String sql = "select id,username,userpwd,score,level,headpic,createdate,email,role,state,active from t_user where id = ?";
		return dh.queryForObject(sql, new UserMapper(), id);
	}

	public int delete(int id) {
		String sql = "DELETE  FROM t_user WHERE id = ?";
		return dh.update(sql, id);
	}

	public int setRole(int uid) {
		String sql = "UPDATE t_user SET role = 2 WHERE id = ?";
		return dh.update(sql, uid);
	}

	/**
	 * 更新用户的数据库信息
	 * @param u
	 * @return
	 */
	public int update(User u) {
		String sql = "UPDATE t_user SET username = ?,userpwd = ?,score = ?,LEVEL = ?,headpic = ?,createdate = ?,email = ?,role = ?,state = ?,active = ? WHERE id = ?";
		return dh.update(sql, u.getUsername(),u.getUserpwd(),u.getScore(),u.getLevel(),u.getHeadpic(),u.getCreatedate(),u.getEmail(),u.getRole(),u.isState(),u.getActive(),u.getId());
	}

	public int getCountByRole(int i) {
		String sql = "SELECT COUNT(*) FROM t_user WHERE role = ?";
		return dh.queryForCount(sql, i);
	}
	
	
}

class UserMapper implements RowMapper<User>{

	public User maprow(ResultSet rs) throws SQLException {
		User u = new User();
		u.setActive(rs.getInt("active"));
		u.setCreatedate(rs.getString("createdate"));
		u.setEmail(rs.getString("email"));
		u.setHeadpic(rs.getString("headpic"));
		u.setId(rs.getInt("id"));
		u.setLevel(rs.getInt("level"));
		u.setRole(rs.getInt("role"));
		u.setScore(rs.getInt("score"));
		u.setUsername(rs.getString("username"));
		u.setUserpwd(rs.getString("userpwd"));
		return u;
	}
	
}
